package daoImpl;

import dao.userDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import pojo.User;
import utils.SessionFactoryUtil;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class userDaoImpl implements userDao {

    public User getUserById(Integer id) {
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        try {
            InputStream inputStream = Resources.getResourceAsStream("SqlMapperConfig.xml");
            SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            Object user = sqlSession.selectOne("getUserById",id);
            System.out.println(user);
            sqlSession.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    public List<User> getUserByUsername(String name) {
        SqlSession sqlSession = SessionFactoryUtil.getSqlSessionFactory().openSession();
        List<User> userlist = sqlSession.selectList("user.getUserByUsername",name);
        sqlSession.close();

        return userlist;
    }

    public void saveUser(User user) {
        SqlSession sqlSession = SessionFactoryUtil.getSqlSessionFactory().openSession();
        sqlSession.insert("saveUser",user);
        sqlSession.commit();
        sqlSession.close();

    }
}
